package com.qs.eggs.integral.activity;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.qs.eggs.Constants;
import com.qs.eggs.R;
import com.qs.eggs.base.activity.BaseActivity;
import com.qs.eggs.integral.DetailList;
import com.qs.eggs.integral.IntegralBean;
import com.qs.eggs.integral.adapter.ExchangeOrderAdapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Response;

public class ExchangeOrderActivity extends BaseActivity {
    public static RecyclerView mRecyclerView;
    public static ExchangeOrderAdapter exchangeOrderAdapter;
    public static ExchangeOrderActivity instance;
    @Override
    protected int getContentView() {
        return R.layout.activity_exchange_order;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        instance=this;
        mRecyclerView=(RecyclerView) findViewById(R.id.recycler);

    }

    @Override
    protected void initData(){
        super.initData();
        getList();

    }

    public static void getList(){
        HashMap<String,String> map=new HashMap<>();
        Gson gson=new Gson();
        String data=gson.toJson(map);
        OkGo.post(Constants.URL_BASE+"changezoneServlet?action=searchchangeZoneOrder")
                .params("data","")
                .params("rows","20")
                .params("page","1")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        JSONObject jsonObject= null;
                        try {
                            jsonObject = new JSONObject(s);
                            JSONArray jsonArray=jsonObject.getJSONArray("rows");
                            Type type = new TypeToken<ArrayList<IntegralBean>>() {
                            }.getType();
                            Type type1 = new TypeToken<ArrayList<DetailList>>() {
                            }.getType();
                            GsonBuilder gsonBuilder = new GsonBuilder();
                            ArrayList<IntegralBean> resultBean = gsonBuilder.create().fromJson(jsonArray.toString(), type);

                            ArrayList<DetailList> lastList=new ArrayList<DetailList>();
                            for (int i=0;i<resultBean.size();i++){
                                ArrayList<DetailList> resultBean1= gsonBuilder.create().fromJson(resultBean.get(i).getDetaillist(), type1);
                                DetailList detailList= resultBean1.get(0);
                                lastList.add(detailList);
                            }
                            //  exchangeOrderAdapter=new ExchangeOrderAdapter(ExchangeOrderActivity.this,lastList);
                            exchangeOrderAdapter=new ExchangeOrderAdapter(instance,resultBean);
                            mRecyclerView.setAdapter(exchangeOrderAdapter);
                            mRecyclerView.setLayoutManager(new LinearLayoutManager(instance));
                            mRecyclerView.setItemAnimator(new DefaultItemAnimator());

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }
}
